package com.audaque.springboot.foshanupload.web.springsecuritycas.config;

import com.audaque.springboot.foshanupload.web.springsecuritycas.handler.UrlAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.cas.authentication.CasAuthenticationProvider;
import org.springframework.security.cas.web.CasAuthenticationFilter;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import java.util.Arrays;

/**
 * @author zgb
 * @desc ...
 * @date 2022-12-12 23:37:15
 */
@Configuration
public class ServiceWebSecuritySecurityCasAuthenticationFilterConfig {


    @Autowired
    private CasServerConfig casServerConfig;

    @Autowired
    private CasServiceConfig casServiceConfig;


    /***
     * CAS认证过滤器
     创建一个引用@Lazy；这告诉Spring使用一个惰性代理，它可以解决循环问题
     */
    @Bean
    public CasAuthenticationFilter casAuthenticationFilter(@Lazy AuthenticationManager authenticationManager, ServiceProperties serviceProperties) {
        CasAuthenticationFilter casAuthenticationFilter = new CasAuthenticationFilter();
        casAuthenticationFilter.setAuthenticationManager(authenticationManager);
        casAuthenticationFilter.setServiceProperties(serviceProperties);
        casAuthenticationFilter.setFilterProcessesUrl(this.casServiceConfig.getLogin());
        casAuthenticationFilter.setContinueChainBeforeSuccessfulAuthentication(false);
        casAuthenticationFilter.setAuthenticationSuccessHandler(new UrlAuthenticationSuccessHandler("/"));
        return casAuthenticationFilter;
    }









}
